layui.config({
    base: "../authority"//引入treeselect.js脚本
}).extend({
    treeSelect: "/treeSelect"
}).use(['treeSelect', 'form', 'jquery', 'util', 'layer'], function () {
    var form = layui.form
        , layer = layui.layer
        , treeSelect = layui.treeSelect
        , $ = layui.$;

    $.ajaxSetup({
        async: false
    })

    $('#close').on("click", function () {
        layerClose();
    })

    //监听提交
    form.on('submit(tijiao)', function (data) {
        data.field.storageAddress = a;
        $.ajax({
            url: '/logisticsStorage/addOrUpdate',
            dataType: 'json',
            type: 'POST',
            data: data.field,
            success: function (resp) {
                if (resp.state == 1) {
                    layerClose();
                    layer.msg(resp.msg);
                    parent.table.reload('storage');
                    parent.layer.msg("成功");
                } else {
                    layer.msg("失败");
                }
            },
            error: function (response) {
                console.log(response);
            }
        });
        return false;
    });

    let id = getQueryString("id");
    if (id != null) {
        $.ajax({
            url: '/logisticsStorage/showUpdate',
            dataType: 'json',
            data: {id: id},
            type: 'POST',
            success: function (resp) {
                address=resp.data.storageAddress;
                if (resp.state === 1) {
                    resp.data.tree=" ";
                    form.val('storage1', resp.data);
                    form.render();
                }
            },
            error: function (response) {
                console.log(response);
            }
        })
    }
    var a;
    var address;
    treeSelect.render({
        elem: '#tree',
        // 数据
        data: "/logisticsArea/selectYi",//json数据
        // 异步加载方式：get/post，默认get
        type: 'get',
        // 占位符
        placeholder: '请填写仓库地址',
        // 是否开启搜索功能：true/false，默认false
        search: true,
        // 点击回调
        click: function (d) {
            a = d.current.id;
        },
        // 加载完成后的回调函数
        success: function (d) {
            //选中节点，根据id筛选
            treeSelect.checkNode('tree1', address);
            //获取zTree对象，可以调用zTree方法
            //var treeObj = treeSelect.zTree('tree');
            //console.log(treeObj);

            //  刷新树结构
            treeSelect.refresh();
        }
    })



})

/**
 得到地址栏的参数
 */
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

